Skip to content

Fix miscompile due to SSA aliasing - #3775

Merged
Stefan J. Wernli (swernli) merged 4 commits into
mainfrom
swernli/issue3769
Oct 2, 2026
Merged

Stefan J. Wernli (swernli) merged 4 commits into
mainfrom
swernli/issue3769

Conversation

@swernli

@swernli Stefan J. Wernli (swernli) commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

This change updates variable mapping logic in RIR passes to differentiate between "deep" and "shallow" mappings, where the former indicates the mapping should be followed through successive links until the terminator operand is reached and the latter indicates no further mapping should be performed. Fixes #3769

Comment thread source/compiler/qsc_rir/src/rir.rs
Comment thread source/compiler/qsc_rir/src/utils.rs
Comment thread source/compiler/qsc/src/codegen/tests/adaptive_profile.rs
Comment thread source/compiler/qsc_rir/src/passes/ssa_transform/tests.rs
Comment thread source/compiler/qsc_rir/src/utils.rs
Comment thread source/compiler/qsc_rir/src/passes/ssa_transform.rs Outdated
@amcasey

Andrew Casey (amcasey) commented Oct 1, 2026 •

Copy link
Copy Markdown
Member
operation Main() : Int {
    use q0 = Qubit();
    use q1 = Qubit();

    X(q0);
    X(q1);

    mutable n = 0;
    if MResetZ(q0) == One {
        n = 1;
    }

    let h = n;

    if MResetZ(q1) == One {
        n = 2;
    }

    h + n
}

Expected: 3
Actual: 4

Speculative explanation (copilot): At ssa_transform.rs:167, only Shallow(n) aliases are redirected when the earlier phi for n is created. An alias that is still Deep(n) remains tied to the mutable ID. Later, ssa_transform.rs:190 can overwrite the shallow mapping created by a branch-local mutation.

@swernli

Copy link
Copy Markdown
Contributor Author
operation Main() : Int {

    use q0 = Qubit();

    use q1 = Qubit();



    X(q0);

    X(q1);



    mutable n = 0;

    if MResetZ(q0) == One {

        n = 1;

    }



    let h = n;



    if MResetZ(q1) == One {

        n = 2;

    }



    h + n

}

Expected: 3

Actual: 4

Speculative explanation (copilot): At ssa_transform.rs:167, only Shallow(n) aliases are redirected when the earlier phi for n is created. An alias that is still Deep(n) remains tied to the mutable ID. Later, ssa_transform.rs:190 can overwrite the shallow mapping created by a branch-local mutation.

This is the resolved in the latest changes.

@swernli
Stefan J. Wernli (swernli) added this pull request to stack #3786 October 2, 2026 17:12
This change updates variable mapping logic in RIR passes to differentiate between "strong" and "weak" mappings, where the former indicates the mapping should be followed through successive links until the terminator operand is reached and the latter indicates no further mapping should be performed.
Fixes #3769

@amcasey Andrew Casey (amcasey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with this code, but the explanation made sense and I couldn't find any remaining issues in buddy testing.

@swernli
Stefan J. Wernli (swernli) added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit 3cba31a Oct 2, 2026
16 checks passed
@swernli
Stefan J. Wernli (swernli) deleted the swernli/issue3769 branch October 2, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Miscompile] Incorrect SSA aliasing

2 participants